Skip to content

Fix signed char arithmetic shift in name escaping#8330

Open
sumleo wants to merge 1 commit intoWebAssembly:mainfrom
sumleo:fix-name-escape-signed-char
Open

Fix signed char arithmetic shift in name escaping#8330
sumleo wants to merge 1 commit intoWebAssembly:mainfrom
sumleo:fix-name-escape-signed-char

Conversation

@sumleo
Copy link
Contributor

@sumleo sumleo commented Feb 16, 2026

Summary

  • In WasmBinaryReader::escape(), the loop variable char c is signed on most platforms (x86, ARM).
  • For bytes >= 0x80, c >> 4 performs an arithmetic right shift, producing negative values that formatNibble converts to incorrect hex characters (e.g., byte 0xFF produces \/f instead of \ff, byte 0xC0 produces wrong high nibble, etc.).
  • Cast to unsigned char before shifting and masking to ensure correct hex encoding of non-ASCII name bytes.

Test plan

  • This is a one-line fix with clear correctness reasoning. The existing test suite passes.

In WasmBinaryReader::escape(), the loop variable 'char c' is signed on
most platforms (x86, ARM). For bytes >= 0x80, 'c >> 4' performs an
arithmetic right shift, producing negative values that formatNibble
converts to wrong hex characters (e.g., 0xFF gives '/' instead of 'f').

Cast to unsigned char before shifting to ensure logical right shift.
@kripken
Copy link
Member

kripken commented Feb 17, 2026

If the old code was incorrect, please add a testcase showing the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants